package com.luomo.wenyuan.modules.wenyuan.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.resource.NoResourceException;
import cn.hutool.system.SystemUtil;
import com.luomo.wenyuan.common.utils.FileUtils;
import com.luomo.wenyuan.common.utils.PageUtils;
import com.luomo.wenyuan.common.utils.R;
import com.luomo.wenyuan.modules.wenyuan.dao.TBookDetailDao;
import com.luomo.wenyuan.modules.wenyuan.entity.TBookEntity;
import com.luomo.wenyuan.modules.wenyuan.service.TBookService;
import com.luomo.wenyuan.modules.wenyuan.vo.TBookVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * @author
 * @email
 * @date 2020-10-19 15:58:40
 */
@RestController
@RequestMapping("generator/tbook")
public class TBookController {

    @Autowired
    private TBookDetailDao tBookDetailDao;

    @Autowired
    private TBookService tBookService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = tBookService.queryPage(params);
        List<TBookEntity> records = (List<TBookEntity>) page.getList();
        if (CollectionUtil.isNotEmpty(records)) {
            List<TBookVo> bookVos = new ArrayList<>();
            TBookVo tBookVo;
            for (TBookEntity record : records) {
                Integer bookId = record.getId();
                tBookVo = new TBookVo();
                BeanUtil.copyProperties(record, tBookVo);
                tBookVo.setStoreNum(tBookDetailDao.countByBookIdAndStatus(bookId, 1));
                tBookVo.setUpNum(tBookDetailDao.countByBookIdAndStatus(bookId, 3));
                tBookVo.setBorrowNum(tBookDetailDao.countByBookIdAndStatus(bookId, 4));
                tBookVo.setCount(tBookDetailDao.countByBookId(bookId));
                bookVos.add(tBookVo);
            }
            page.setList(bookVos);
        }
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id) {
        TBookEntity tBook = tBookService.getById(id);
        return R.ok().put("tBook", tBook);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody TBookEntity tBook) {
        tBookService.save(tBook);
        return R.ok().put("data", tBook.getId());
    }

    @PostMapping(value = "/upload/{id}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R upload(@PathVariable("id") Integer id, MultipartFile file) {
        TBookEntity book = tBookService.getById(id);
        if (book == null) {
            throw new NoResourceException("");
        }
        String picPath;
        boolean windows = SystemUtil.getOsInfo().isWindows();
        if (windows) {
            picPath = "C:\\wenyuan\\";
        } else {
            picPath = "/wenyuan/";
        }
        String path = picPath + "book-img" + File.separator + id;
        String absolutePath = FileUtils.saveFile(file, path);
        book.setImg(absolutePath);
        tBookService.updateById(book);
        return R.ok();
    }

    @GetMapping(value = "/img/{id}", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public ResponseEntity getLogo(@PathVariable("id") int id) {
        TBookEntity data = tBookService.getById(id);
        if (data == null) {
            throw new NoResourceException("");
        }
        String path = data.getImg();
        return FileUtils.getFile(path);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody TBookEntity tBook) {
        Integer id = tBook.getId();
        tBook.setImg(tBookService.getById(id).getImg());
        tBookService.updateById(tBook);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids) {
        tBookService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
